package com.stress.testing.plugin.es.interceptor;

import com.stress.testing.common.StressTesting;
import com.stress.testing.common.TraceContextHolder;
import com.stress.testing.common.interceptor.AbstractInterceptor;
import com.stress.testing.plugin.es.strategy.RequestIndexRenameStrategy;
import org.elasticsearch.action.ActionRequest;

import java.lang.reflect.Method;

public class ElasticsearchTransportInterceptor extends AbstractInterceptor {
    @Override
    public void before(Method method, Object[] args, Object zuper, Object thiz) {
        if (!StressTesting.enable(getAgentConfig(), getPluginConfig(), TraceContextHolder.getTraceContext())) {
            return;
        }

        if (args == null || args.length != 3) {
            return;
        }

        Object arg = args[1];
        if (arg == null || !(arg instanceof ActionRequest)) {
            return;
        }

        RequestIndexRenameStrategy.getForClass(arg.getClass(), getAgentConfig(), getPluginConfig()).renameIndex(arg);

        /*if (arg instanceof GetRequest) {

        } else if (arg instanceof UpdateRequest) {

        } else if (arg instanceof IndexRequest) {

        } else if (arg instanceof DeleteRequest) {

        } else if (arg instanceof TermVectorsRequest) {

        } else if (arg instanceof ExplainRequest) {

        } else if (arg instanceof MultiGetRequest) {

        } else if (arg instanceof MultiTermVectorsRequest) {

        } else if (arg instanceof SearchRequest) {

        } else if (arg instanceof BulkRequest) {

        }*/
    }
}
